﻿/**
<code>
  <author name="Zealic" email="zealic(at)gmail.com" type="Creator"/>
</code>
**/
using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;


namespace Zealib.Transform
{
    /// <summary>
    /// TypeConverter transformer adapter factory.
    /// </summary>
    internal sealed class TypeConverterTransformerFactory : ITransformerFactory
    {
        public bool TryCreate(Type sourceType, out ITransformer result)
        {
            if (sourceType == null) throw new ArgumentNullException("sourceType");
            result = null;
            TypeConverter converter = TypeDescriptor.GetConverter(sourceType);
            if (converter == null)
                return false;
            if ((converter.GetType() == typeof(TypeConverter) &&
                sourceType != typeof(InstanceDescriptor)))
                return false;
            result = new TypeConverterTransformer(converter);
            return true;
        }
    }

}
